Imports System.Collections

Partial Public Class pages_Products_usercontrols_UCProducts : Inherits UserControl

#Region "Events"
    Public Event AddClick()
    Public Event ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer)
#End Region

#Region "Members"
    Private m_list As CProductList
#End Region

#Region "Interface"
    Public Sub Display(ByVal products As CProductList, showDetails As Boolean, showPictures As Boolean, showPricing As Boolean, showContent As Boolean, showTotals As Boolean, showOrder As Boolean) 'Full list, paging done internally
        m_list = products

        'Show/Hide Columns
        colNumber.Visible = products.Count > 0

        plhDetails.Visible = showDetails
        plhContents.Visible = showContent
        plhPayments.Visible = showTotals
        plhPricing.Visible = showPricing
        plhOrder.Visible = showOrder

        'Display
        plh.Controls.Clear() 'Only needed for ajax postbacks
        Dim sorted As IList = Nothing 'Fixes the numbering to reflect a user-sorted list (querystring sortBy)
        Dim page As IList = ctrlPaging.Display(products, sorted) 'In-Memory paging, also outputs the sorted list
        For Each i As CProduct In page
            UCProduct(plh).Display(i, sorted, showDetails, showPictures, showPricing, showContent, showTotals, showOrder)
        Next
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCProduct(ByVal target As Control) As pages_products_usercontrols_UCProduct
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.UCProduct)
        target.Controls.Add(ctrl)
        Return CType(ctrl, pages_products_usercontrols_UCProduct)
    End Function
#End Region

#Region "Event Handlers"
    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAdd.Click
        RaiseEvent AddClick
    End Sub
    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExport.Click
        m_list.ExportToCsv(Response)
    End Sub
    Protected Sub btnResort_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles btnSortByProductName.Click, btnSortByType.Click, btnSortByProductDescription.Click, btnSortByProductPrice.Click, btnSortByPaypalButton.Click, btnSortByProductIsActive.Click, btnSortByProductIsFree.Click, btnSortByProductCreated.Click
        'Toggle descending if necessary
        Dim sortBy As String = CType(sender, LinkButton).CommandArgument
        Dim descending As Boolean = ctrlPaging.IsDescending
        Dim currentSort As String = ctrlPaging.SortColumn
        If Not String.IsNullOrEmpty(currentSort) Then
            If currentSort = sortBy Then descending = Not descending
        End If

        'Bubble up as event, search page will add filter info and redirect
        RaiseEvent ResortClick(sortBy, descending, ctrlPaging.Info.PageIndex + 1)
    End Sub
#End Region

#Region "Paging"
    Public ReadOnly Property Info() As CPagingInfo
        Get
            Return ctrlPaging.Info
        End Get
    End Property
    Public Property QueryString() As String
        Get
            Return ctrlPaging.QueryString
        End Get
        Set(ByVal value As String)
            ctrlPaging.QueryString = value
        End Set
    End Property
    Public Property PageSize() As Integer
        Get
            Return ctrlPaging.PageSize
        End Get
        Set(ByVal value As Integer)
            ctrlPaging.PageSize = value
        End Set
    End Property
#End Region

    Protected Sub btnResetOrder_Click(sender As Object, e As System.EventArgs) Handles btnResetOrder.Click
        m_list.SortAlphabetically()
        Response.Redirect(Request.RawUrl)
    End Sub
End Class
